﻿/*************************************************************************
* Copyright(c) 2020 - Present ShenZhen EscopeTech

* ShenZhen EscopeTech Incoperated CONFIDENTIAL
* ________________________________________________________________________
*
* [2020] - Present ShenZhen EscopeTech Incoperated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of ShenZhen EscopeTech Incoperated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to ShenZhen EscopeTech Incoperated
* and its suppliers and may be covered by P.R.C. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from ShenZhen EscopeTech Incoperated.
*
*************************************************************************/

#include "DLogInfoFormat.h"

#include <QThread>
#include <QVariant>

// struct CustomInfo{
//    friend inline QDebug operator<<(QDebug out, const CustomInfo& info)
//    {
//        out <<" CustomInfo("1 2 3") ";
//        return out;
//    }
//};

DLogInfoFormatBase::DLogInfoFormatBase(const char* strMsgLogFunc,
                                       const int& strMsgLogLine,
                                       const char* strMsgLogFile,
                                       const QtMsgType& msgType)
    : m_debug(&m_strArgs)
    , m_strMsgLogFunc(strMsgLogFunc)
    , m_strMsgLogLine(strMsgLogLine)
    , m_strMsgLogFile(strMsgLogFile)
    , m_msgType(msgType)
{
    m_threadID = (qint64)(QThread::currentThreadId());
}

void DLogInfoFormatBase::outputLog(const QString& strTag, const bool&& bLogTime)
{
    QString strHeader;
    QString strArgs;
    if (!strTag.isEmpty())  // tag
    {
        strHeader = QString("%1::%2 (%3)line (%4)thread")
                        .arg(m_strMsgLogFile)
                        .arg(m_strMsgLogFunc)
                        .arg(m_strMsgLogLine)
                        .arg(QString::number(m_threadID, 16));

        if (bLogTime)
        {
            strHeader += QString(" (%1)ms").arg(m_timeElapse.elapsedMs());
        }

        strHeader = strHeader + " " + strTag;
    }
    else if (!m_strArgs.isEmpty())
    {
        strArgs = QString(">%1").arg(m_strArgs);
    }

    if (strHeader.isEmpty() && !strArgs.isEmpty())
    {
        if (QtDebugMsg == m_msgType)
        {
            QMessageLogger("", 0, "").debug(qUtf8Printable(strArgs));
        }
        else if (QtInfoMsg == m_msgType)
        {
            QMessageLogger("", 0, "").info(qUtf8Printable(strArgs));
        }
        else if (QtWarningMsg == m_msgType)
        {
            QMessageLogger("", 0, "").warning(qUtf8Printable(strArgs));
        }
        else if (QtCriticalMsg == m_msgType)
        {
            QMessageLogger("", 0, "").critical(qUtf8Printable(strArgs));
        }
        else if (QtFatalMsg == m_msgType)
        {
            QMessageLogger("", 0, "").fatal(qUtf8Printable(strArgs));
        }
        else
        {
            QMessageLogger("", 0, "").info(qUtf8Printable(strArgs));
        }
    }
    else if (!strHeader.isEmpty())
    {
        if (QtDebugMsg == m_msgType)
        {
            QMessageLogger("Tag", 0, "").debug(qUtf8Printable(strHeader));
        }
        else if (QtInfoMsg == m_msgType)
        {
            QMessageLogger("Tag", 0, "").info(qUtf8Printable(strHeader));
        }
        else if (QtWarningMsg == m_msgType)
        {
            QMessageLogger("Tag", 0, "").warning(qUtf8Printable(strHeader));
        }
        else if (QtCriticalMsg == m_msgType)
        {
            QMessageLogger("Tag", 0, "").critical(qUtf8Printable(strHeader));
        }
        else if (QtFatalMsg == m_msgType)
        {
            QMessageLogger("Tag", 0, "").fatal(qUtf8Printable(strHeader));
        }
        else
        {
            QMessageLogger("Tag", 0, "").info(qUtf8Printable(strHeader));
        }
    }
}

DLogInfoFormatBegin::DLogInfoFormatBegin(const char* strMsgLogFunc,
                                         const int& strMsgLogLine,
                                         const char* strMsgLogFile,
                                         const QtMsgType& msgType)
    : DLogInfoFormatBase(strMsgLogFunc, strMsgLogLine, strMsgLogFile, msgType)

{
    outputLog("Begin", false);
}

DLogInfoFormatBegin::~DLogInfoFormatBegin() { outputLog("", true); }

DLogInfoFormatEnd::DLogInfoFormatEnd(const char* strMsgLogFunc,
                                     const int& strMsgLogLine,
                                     const char* strMsgLogFile,
                                     const QtMsgType& msgType)
    : DLogInfoFormatBase(strMsgLogFunc, strMsgLogLine, strMsgLogFile, msgType)
{
}

DLogInfoFormatEnd::~DLogInfoFormatEnd() { outputLog("End", true); }
